import com.baidu.aip.imagesearch.AipImageSearch;
import org.json.JSONObject;

import java.util.HashMap;

/**
 * @author sjw
 * @className Main
 * @date 2020/12/23 14:39
 * @description: TODO
 */
public class Main {

    //设置APPID/AK/SK
    public static final String APP_ID = "23256724";
    public static final String API_KEY = "wr94TWkiX62dMDskm7g4gPtX";
    public static final String SECRET_KEY = "GAlHZWDayzhtzIpGGLAqNrlIev89W71G";

    public static void main(String[] args) {
// 初始化一个AipImageSearch
        AipImageSearch client = new AipImageSearch(APP_ID, API_KEY, SECRET_KEY);

        // 可选：设置网络连接参数
        client.setConnectionTimeoutInMillis(2000);
        client.setSocketTimeoutInMillis(60000);

        // 可选：设置代理服务器地址, http和socket二选一，或者均不设置
        //client.setHttpProxy("proxy_host", proxy_port);  // 设置http代理
        //client.setSocketProxy("proxy_host", proxy_port);  // 设置socket代理

        // 可选：设置log4j日志输出格式，若不设置，则使用默认配置
        // 也可以直接通过jvm启动参数设置此环境变量
        System.setProperty("aip.log4j.conf", "path/to/your/log4j.properties");

        // 调用接口
        //String path = "test.jpg";
        //JSONObject res = client.sameHqSearch(path, new HashMap<String, String>());
        //System.out.println(res.toString(2));

        String imgUrl = "http://tsqjcdn.eallerp.com/eagle/3257/house/2020-12-23/ef3a0a6ca0f52e18141dd1d287bb3749c2730e56.png?x-oss-process=image/resize,w_800/watermark,image_d2F0ZXIvMzI1N18xNTk3MTkzNjMyLnBuZw==";
        String imgUrl1="http://tsqjcdn.eallerp.com/eagle/3257/house/2020-12-23/9bc428c286159b00816fed950f7e79abfd9cd6eb.jpg?x-oss-process=image/resize,w_800/watermark,image_d2F0ZXIvMzI1N18xNTk3MTkzNjMyLnBuZw==";
        //upLoadImg(client,imgUrl,"M-0-001","001");
        searchSmlr(client, imgUrl1);

    }

    private static void upLoadImg(AipImageSearch client, String imgUrl, String brief, String imgId) {
        HashMap<String, String> options = new HashMap<String, String>();
        options.put("tags", imgId);

//        String brief = "{\"name\":\"周杰伦\", \"id\":\"666\"}";
//
//        // 参数为本地路径
//        String image = "test.jpg";
//        JSONObject res = client.sameHqAdd(image, brief, options);
//        System.out.println(res.toString(2));
//
//        // 参数为二进制数组
//        byte[] file = readFile("test.jpg");
//        res = client.sameHqAdd(file, brief, options);
//        System.out.println(res.toString(2));

        // 相同图检索—入库, 图片参数为远程url图片
        JSONObject res = client.similarAddUrl(imgUrl, brief, options);
        System.out.println(res.toString(2));
    }


    private static void searchSmlr(AipImageSearch client, String imgUrl) {
        JSONObject jsonObject = client.similarSearchUrl(imgUrl, null);
        System.out.println(jsonObject.toString(2));
    }


}
